<template>
  <Dialog v-model="dialogVisible" title="数据权限" width="800" class="enhanced-dialog">
    <!-- 弹窗头部 -->
    <div class="dialog-header">
      <div class="header-icon">
        <Icon icon="ep:coin" />
      </div>
      <div class="header-content">
        <h4 class="dialog-title">数据权限</h4>
        <p class="dialog-subtitle">为角色配置数据访问范围</p>
      </div>
    </div>

    <!-- 表单内容 -->
    <div class="form-container">
      <el-form
        ref="formRef"
        v-loading="formLoading"
        :model="formData"
        label-width="80px"
        class="enhanced-form"
      >
        <!-- 角色信息 -->
        <div class="form-section">
          <div class="section-title">
            <Icon icon="ep:user" class="section-icon" />
            <span>角色信息</span>
          </div>

          <el-form-item label="角色名称" class="form-item">
            <el-tag class="enhanced-tag role-tag">
              <Icon icon="ep:user" class="tag-icon" />
              {{ formData.name }}
            </el-tag>
          </el-form-item>

          <el-form-item label="角色标识" class="form-item">
            <el-tag class="enhanced-tag code-tag">
              <Icon icon="ep:key" class="tag-icon" />
              {{ formData.code }}
            </el-tag>
          </el-form-item>
        </div>

        <!-- 权限范围 -->
        <div class="form-section">
          <div class="section-title">
            <Icon icon="ep:coin" class="section-icon" />
            <span>权限范围</span>
          </div>

          <el-form-item label="权限范围" class="form-item">
            <el-select v-model="formData.dataScope" class="enhanced-select">
              <template #prefix>
                <Icon icon="ep:setting" />
              </template>
              <el-option
                v-for="item in getIntDictOptions(DICT_TYPE.SYSTEM_DATA_SCOPE)"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
                <div class="option-item">
                  <Icon icon="ep:circle-check" class="option-icon" />
                  <span>{{ item.label }}</span>
                </div>
              </el-option>
            </el-select>
          </el-form-item>
        </div>
      </el-form>

      <!-- 部门权限 -->
      <div
        v-if="formData.dataScope === SystemDataScopeEnum.DEPT_CUSTOM"
        class="dept-permission-section"
      >
        <div class="section-title">
          <Icon icon="ep:office-building" class="section-icon" />
          <span>部门权限</span>
        </div>

        <el-card class="dept-card">
          <template #header>
            <div class="card-header">
              <div class="control-group">
                <div class="control-item">
                  <Icon icon="ep:select" class="control-icon" />
                  <span class="control-label">全选/全不选:</span>
                  <el-switch
                    v-model="treeNodeAll"
                    active-text="是"
                    inactive-text="否"
                    inline-prompt
                    @change="handleCheckedTreeNodeAll()"
                    class="enhanced-switch"
                  />
                </div>
                <div class="control-item">
                  <Icon icon="ep:expand" class="control-icon" />
                  <span class="control-label">全部展开/折叠:</span>
                  <el-switch
                    v-model="deptExpand"
                    active-text="展开"
                    inactive-text="折叠"
                    inline-prompt
                    @change="handleCheckedTreeExpand"
                    class="enhanced-switch"
                  />
                </div>
                <div class="control-item">
                  <Icon icon="ep:connection" class="control-icon" />
                  <span class="control-label">父子联动:</span>
                  <el-switch
                    v-model="checkStrictly"
                    active-text="是"
                    inactive-text="否"
                    inline-prompt
                    class="enhanced-switch"
                  />
                </div>
              </div>
              <div class="control-tip">
                <Icon icon="ep:info" class="tip-icon" />
                <span>选中父节点，自动选择子节点</span>
              </div>
            </div>
          </template>

          <div class="tree-container">
            <el-tree
              ref="treeRef"
              :check-strictly="!checkStrictly"
              :data="deptOptions"
              :props="defaultProps"
              default-expand-all
              empty-text="加载中，请稍后"
              node-key="id"
              show-checkbox
              class="enhanced-tree"
            />
          </div>
        </el-card>
      </div>
    </div>

    <!-- 弹窗底部 -->
    <template #footer>
      <div class="dialog-footer">
        <el-button :disabled="formLoading" type="primary" @click="submitForm" class="submit-btn">
          <Icon icon="ep:check" class="btn-icon" />
          确 定
        </el-button>
        <el-button @click="dialogVisible = false" class="cancel-btn">
          <Icon icon="ep:close" class="btn-icon" />
          取 消
        </el-button>
      </div>
    </template>
  </Dialog>
</template>

<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { defaultProps, handleTree } from '@/utils/tree'
import { SystemDataScopeEnum } from '@/utils/constants'
import * as RoleApi from '@/api/system/role'
import * as DeptApi from '@/api/system/dept'
import * as PermissionApi from '@/api/system/permission'

defineOptions({ name: 'SystemRoleDataPermissionForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = reactive({
  id: 0,
  name: '',
  code: '',
  dataScope: undefined,
  dataScopeDeptIds: []
})
const formRef = ref() // 表单 Ref
const deptOptions = ref<any[]>([]) // 部门树形结构
const deptExpand = ref(true) // 展开/折叠
const treeRef = ref() // 菜单树组件 Ref
const treeNodeAll = ref(false) // 全选/全不选
const checkStrictly = ref(true) // 是否严格模式，即父子不关联

/** 打开弹窗 */
const open = async (row: RoleApi.RoleVO) => {
  dialogVisible.value = true
  resetForm()
  // 加载 Dept 列表。注意，必须放在前面，不然下面 setChecked 没数据节点
  deptOptions.value = handleTree(await DeptApi.getSimpleDeptList())
  // 设置数据
  formData.id = row.id
  formData.name = row.name
  formData.code = row.code
  formData.dataScope = row.dataScope
  row.dataScopeDeptIds?.forEach((deptId: number) => {
    treeRef.value.setChecked(deptId, true, false)
  })
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  formLoading.value = true
  try {
    const data = {
      roleId: formData.id,
      dataScope: formData.dataScope,
      dataScopeDeptIds:
        formData.dataScope !== SystemDataScopeEnum.DEPT_CUSTOM
          ? []
          : treeRef.value.getCheckedKeys(false)
    }
    await PermissionApi.assignRoleDataScope(data)
    message.success(t('common.updateSuccess'))
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  // 重置选项
  treeNodeAll.value = false
  deptExpand.value = true
  checkStrictly.value = true
  // 重置表单
  formData.value = {
    id: 0,
    name: '',
    code: '',
    dataScope: undefined,
    dataScopeDeptIds: []
  }
  treeRef.value?.setCheckedNodes([])
  formRef.value?.resetFields()
}

/** 全选/全不选 */
const handleCheckedTreeNodeAll = () => {
  treeRef.value.setCheckedNodes(treeNodeAll.value ? deptOptions.value : [])
}

/** 展开/折叠全部 */
const handleCheckedTreeExpand = () => {
  const nodes = treeRef.value?.store.nodesMap
  for (let node in nodes) {
    if (nodes[node].expanded === deptExpand.value) {
      continue
    }
    nodes[node].expanded = deptExpand.value
  }
}
</script>

<style lang="scss" scoped>
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$info-color: #909399;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

/* 弹窗样式 */
.enhanced-dialog {
  :deep(.el-dialog) {
    border-radius: $border-radius;
    overflow: hidden;

    @media (max-width: 768px) {
      width: 95% !important;
      margin: 0 auto;
    }
  }

  :deep(.el-dialog__header) {
    padding: 0;
    border-bottom: none;
  }

  :deep(.el-dialog__body) {
    padding: 0;
  }

  :deep(.el-dialog__footer) {
    padding: 0;
    border-top: none;
  }

  :deep(.el-dialog__headerbtn) {
    top: 16px;
    right: 16px;
    z-index: 10;

    .el-dialog__close {
      color: white;
      font-size: 18px;

      &:hover {
        color: rgba(255, 255, 255, 0.8);
      }
    }
  }
}

/* 弹窗头部 */
.dialog-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
    gap: 8px;
    text-align: center;
  }
}

.header-icon {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 36px;
    height: 36px;
    font-size: 16px;
  }
}

.header-content {
  flex: 1;
  margin-left: 12px;

  @media (max-width: 768px) {
    margin-left: 0;
  }
}

.dialog-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.dialog-subtitle {
  font-size: 13px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 表单容器 */
.form-container {
  padding: 24px;
  background: white;
  max-height: 70vh;
  overflow-y: auto;

  @media (max-width: 768px) {
    padding: 16px;
    max-height: 60vh;
  }

  /* 滚动条美化 */
  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.05);
    border-radius: 3px;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(0, 82, 204, 0.3);
    border-radius: 3px;
    transition: all 0.3s ease;

    &:hover {
      background: rgba(0, 82, 204, 0.5);
    }
  }
}

.enhanced-form {
  .form-section {
    margin-bottom: 32px;

    &:last-child {
      margin-bottom: 0;
    }

    @media (max-width: 768px) {
      margin-bottom: 24px;
    }
  }

  .section-title {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 20px;
    padding-bottom: 12px;
    border-bottom: 2px solid $primary-light;
    font-size: 16px;
    font-weight: 600;
    color: $text-primary;

    @media (max-width: 768px) {
      font-size: 14px;
      margin-bottom: 16px;
      padding-bottom: 8px;
    }
  }

  .section-icon {
    color: $primary-color;
    font-size: 18px;

    @media (max-width: 768px) {
      font-size: 16px;
    }
  }

  .form-item {
    margin-bottom: 20px;

    @media (max-width: 768px) {
      margin-bottom: 16px;
    }

    :deep(.el-form-item__label) {
      font-weight: 600;
      color: $text-regular;
      padding-bottom: 8px;

      @media (max-width: 768px) {
        font-size: 14px;
        width: 100% !important;
        text-align: left !important;
        margin-bottom: 8px;
      }
    }

    :deep(.el-form-item__content) {
      @media (max-width: 768px) {
        margin-left: 0 !important;
      }
    }
  }

  // H5适配：表单项垂直布局
  @media (max-width: 768px) {
    :deep(.el-form-item) {
      display: block;
    }
  }
}

/* 标签样式 */
.enhanced-tag {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border-radius: $border-radius-small;
  font-weight: 500;
  font-size: 14px;
  border: none;
  transition: all 0.3s ease;

  @media (max-width: 768px) {
    padding: 6px 10px;
    font-size: 13px;
  }

  &.role-tag {
    background: linear-gradient(135deg, $primary-light 0%, rgba(0, 82, 204, 0.05) 100%);
    color: $primary-color;
    border: 1px solid $primary-border;
  }

  &.code-tag {
    background: linear-gradient(135deg, rgba(103, 194, 58, 0.1) 0%, rgba(103, 194, 58, 0.05) 100%);
    color: $success-color;
    border: 1px solid rgba(103, 194, 58, 0.3);
  }
}

.tag-icon {
  font-size: 14px;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 选择器样式 */
.enhanced-select {
  width: 100%;

  :deep(.el-select__wrapper) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    padding: 8px 16px;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focused {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
      border-color: $primary-color;
    }
  }
}

/* 选项样式 */
.option-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
}

.option-icon {
  font-size: 14px;
  color: $primary-color;
}

/* 部门权限区域 */
.dept-permission-section {
  margin-top: 24px;

  @media (max-width: 768px) {
    margin-top: 20px;
  }

  .section-title {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 20px;
    padding-bottom: 12px;
    border-bottom: 2px solid $primary-light;
    font-size: 16px;
    font-weight: 600;
    color: $text-primary;

    @media (max-width: 768px) {
      font-size: 14px;
      margin-bottom: 16px;
      padding-bottom: 8px;
    }
  }

  .section-icon {
    color: $primary-color;
    font-size: 18px;

    @media (max-width: 768px) {
      font-size: 16px;
    }
  }
}

/* 部门卡片样式 */
.dept-card {
  border-radius: $border-radius;
  border: 1px solid $border-light;
  box-shadow: $shadow-base;
  overflow: hidden;

  :deep(.el-card__header) {
    padding: 16px 20px;
    background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%);
    border-bottom: 1px solid $border-light;

    @media (max-width: 768px) {
      padding: 12px 16px;
    }
  }

  :deep(.el-card__body) {
    padding: 0;
  }
}

.card-header {
  .control-group {
    display: flex;
    gap: 24px;
    flex-wrap: wrap;
    margin-bottom: 16px;

    @media (max-width: 768px) {
      flex-direction: column;
      gap: 16px;
      margin-bottom: 12px;
    }
  }

  .control-item {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 1;
    min-width: 200px;

    @media (max-width: 768px) {
      min-width: auto;
      justify-content: space-between;
    }
  }

  .control-icon {
    color: $primary-color;
    font-size: 16px;
    flex-shrink: 0;

    @media (max-width: 768px) {
      font-size: 14px;
    }
  }

  .control-label {
    font-size: 14px;
    font-weight: 500;
    color: $text-regular;
    flex-shrink: 0;

    @media (max-width: 768px) {
      font-size: 13px;
    }
  }

  .control-tip {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 12px;
    background: rgba(0, 82, 204, 0.05);
    border-radius: $border-radius-small;
    font-size: 12px;
    color: $text-secondary;

    @media (max-width: 768px) {
      font-size: 11px;
      padding: 6px 10px;
    }
  }

  .tip-icon {
    color: $primary-color;
    font-size: 14px;
    flex-shrink: 0;

    @media (max-width: 768px) {
      font-size: 12px;
    }
  }
}

/* 开关样式 */
.enhanced-switch {
  :deep(.el-switch__core) {
    background-color: $border-color;
    border-color: $border-color;

    &.is-checked {
      background-color: $primary-color;
      border-color: $primary-color;
    }
  }

  :deep(.el-switch__action) {
    background-color: white;
  }
}

/* 树形容器 */
.tree-container {
  max-height: 400px;
  overflow-y: auto;
  padding: 16px 20px;
  background: white;

  @media (max-width: 768px) {
    max-height: 300px;
    padding: 12px 16px;
  }

  /* 滚动条美化 */
  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.05);
    border-radius: 3px;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(0, 82, 204, 0.3);
    border-radius: 3px;
    transition: all 0.3s ease;

    &:hover {
      background: rgba(0, 82, 204, 0.5);
    }
  }
}

/* 树形组件样式 */
.enhanced-tree {
  :deep(.el-tree-node) {
    margin-bottom: 4px;

    &:last-child {
      margin-bottom: 0;
    }

    .el-tree-node__content {
      height: 36px;
      border-radius: $border-radius-small;
      transition: all 0.3s ease;
      padding: 0 8px;
      margin-bottom: 2px;
      border: 1px solid transparent;

      &:hover {
        background: $primary-light;
        border-color: $primary-border;
        transform: translateX(2px);
      }

      @media (max-width: 768px) {
        height: 32px;
        padding: 0 6px;
      }
    }

    .el-tree-node__expand-icon {
      color: $primary-color;
      font-size: 14px;
      transition: all 0.3s ease;
      padding: 3px;
      border-radius: 3px;

      &:hover {
        background: $primary-light;
      }

      &.is-leaf {
        color: transparent;
      }

      @media (max-width: 768px) {
        font-size: 12px;
        padding: 2px;
      }
    }

    .el-tree-node__label {
      font-size: 14px;
      color: $text-regular;
      font-weight: 500;
      transition: all 0.3s ease;
      padding: 4px 8px;
      border-radius: 4px;
      flex: 1;
      display: flex;
      align-items: center;
      gap: 6px;

      &::before {
        content: '';
        width: 4px;
        height: 4px;
        background: $primary-color;
        border-radius: 50%;
        opacity: 0.6;
        transition: all 0.3s ease;
      }

      @media (max-width: 768px) {
        font-size: 13px;
        padding: 3px 6px;

        &::before {
          width: 3px;
          height: 3px;
        }
      }
    }

    .el-checkbox {
      margin-right: 8px;

      :deep(.el-checkbox__input) {
        .el-checkbox__inner {
          border-color: $primary-border;
          border-radius: 4px;
          transition: all 0.3s ease;

          &:hover {
            border-color: $primary-color;
          }
        }

        &.is-checked {
          .el-checkbox__inner {
            background-color: $primary-color;
            border-color: $primary-color;
          }
        }

        &.is-indeterminate {
          .el-checkbox__inner {
            background-color: $primary-color;
            border-color: $primary-color;
          }
        }
      }

      @media (max-width: 768px) {
        margin-right: 6px;

        :deep(.el-checkbox__input) {
          .el-checkbox__inner {
            width: 14px;
            height: 14px;

            &::after {
              width: 3px;
              height: 6px;
            }
          }
        }
      }
    }
  }

  // 子节点缩进样式
  :deep(.el-tree-node__children) {
    .el-tree-node {
      .el-tree-node__content {
        margin-left: 18px;
        position: relative;

        &::before {
          content: '';
          position: absolute;
          left: -9px;
          top: 50%;
          width: 6px;
          height: 1px;
          background: $primary-border;
        }

        @media (max-width: 768px) {
          margin-left: 14px;

          &::before {
            left: -7px;
            width: 4px;
          }
        }
      }
    }
  }

  // 空状态样式
  :deep(.el-tree__empty-block) {
    padding: 40px 0;

    .el-tree__empty-text {
      color: $text-secondary;
      font-size: 14px;
      position: relative;

      &::before {
        content: '🏢';
        display: block;
        font-size: 32px;
        margin-bottom: 8px;
      }

      @media (max-width: 768px) {
        font-size: 13px;

        &::before {
          font-size: 28px;
          margin-bottom: 6px;
        }
      }
    }
  }
}

/* 弹窗底部 */
.dialog-footer {
  display: flex;
  justify-content: center;
  gap: 12px;
  padding: 20px 24px;
  background: #fafbff;
  border-top: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
  }
}

.submit-btn {
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  border: none;
  border-radius: $border-radius-small;
  padding: 12px 24px;
  font-weight: 500;
  color: white;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;

  &:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
  }

  &:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
  }

  @media (max-width: 768px) {
    width: 100%;
    justify-content: center;
  }

  .btn-icon {
    font-size: 14px;
  }
}

.cancel-btn {
  border: 2px solid $border-color;
  color: $text-regular;
  border-radius: $border-radius-small;
  padding: 12px 24px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;

  &:hover {
    border-color: $primary-color;
    color: $primary-color;
    background: $primary-light;
  }

  @media (max-width: 768px) {
    width: 100%;
    justify-content: center;
  }

  .btn-icon {
    font-size: 14px;
  }
}

/* 加载状态优化 */
:deep(.el-loading-mask) {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);

  .el-loading-spinner {
    .path {
      stroke: $primary-color;
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .form-container {
    background: #374151;
  }

  .dialog-footer {
    background: #4b5563;
    border-color: #6b7280;
  }

  .section-title {
    color: #f9fafb;
    border-color: rgba(96, 165, 250, 0.3);
  }

  .enhanced-form {
    .form-item {
      :deep(.el-form-item__label) {
        color: #d1d5db;
      }
    }
  }

  .enhanced-tag {
    &.role-tag {
      background: linear-gradient(
        135deg,
        rgba(96, 165, 250, 0.1) 0%,
        rgba(96, 165, 250, 0.05) 100%
      );
      color: #60a5fa;
      border-color: rgba(96, 165, 250, 0.3);
    }

    &.code-tag {
      background: linear-gradient(
        135deg,
        rgba(52, 211, 153, 0.1) 0%,
        rgba(52, 211, 153, 0.05) 100%
      );
      color: #34d399;
      border-color: rgba(52, 211, 153, 0.3);
    }
  }

  .enhanced-select {
    :deep(.el-select__wrapper) {
      background: #4b5563;
      border-color: #6b7280;

      .el-input__inner {
        color: #f9fafb;
      }
    }
  }

  .option-icon {
    color: #60a5fa;
  }

  .dept-card {
    background: #4b5563;
    border-color: #6b7280;

    :deep(.el-card__header) {
      background: linear-gradient(135deg, #4b5563 0%, #374151 100%);
      border-bottom-color: #6b7280;
    }
  }

  .control-label {
    color: #f9fafb;
  }

  .control-icon {
    color: #60a5fa;
  }

  .control-tip {
    background: rgba(96, 165, 250, 0.1);
    color: #d1d5db;
  }

  .tip-icon {
    color: #60a5fa;
  }

  .tree-container {
    background: #4b5563;
  }

  .enhanced-tree {
    :deep(.el-tree-node) {
      .el-tree-node__content {
        &:hover {
          background: rgba(96, 165, 250, 0.1);
          border-color: rgba(96, 165, 250, 0.3);
        }
      }

      .el-tree-node__expand-icon {
        color: #60a5fa;

        &:hover {
          background: rgba(96, 165, 250, 0.1);
        }
      }

      .el-tree-node__label {
        color: #f9fafb;

        &::before {
          background: #60a5fa;
        }
      }

      .el-checkbox {
        :deep(.el-checkbox__input) {
          .el-checkbox__inner {
            border-color: rgba(96, 165, 250, 0.3);
            background: #4b5563;

            &:hover {
              border-color: #60a5fa;
            }
          }

          &.is-checked {
            .el-checkbox__inner {
              background-color: #60a5fa;
              border-color: #60a5fa;
            }
          }

          &.is-indeterminate {
            .el-checkbox__inner {
              background-color: #60a5fa;
              border-color: #60a5fa;
            }
          }
        }
      }
    }

    :deep(.el-tree-node__children) {
      .el-tree-node {
        .el-tree-node__content {
          &::before {
            background: rgba(96, 165, 250, 0.3);
          }
        }
      }
    }

    :deep(.el-tree__empty-block) {
      .el-tree__empty-text {
        color: #9ca3af;
      }
    }
  }

  .enhanced-switch {
    :deep(.el-switch__core) {
      background-color: #6b7280;
      border-color: #6b7280;

      &.is-checked {
        background-color: #60a5fa;
        border-color: #60a5fa;
      }
    }
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .dialog-header {
    padding: 12px 16px;
  }

  .form-container {
    padding: 12px;
  }

  .enhanced-form {
    .form-section {
      margin-bottom: 20px;
    }

    .section-title {
      font-size: 13px;
      margin-bottom: 12px;
      padding-bottom: 6px;
    }

    .form-item {
      margin-bottom: 12px;

      :deep(.el-form-item__label) {
        font-size: 13px;
      }
    }
  }

  .dept-permission-section {
    margin-top: 16px;

    .section-title {
      font-size: 13px;
      margin-bottom: 12px;
      padding-bottom: 6px;
    }
  }

  .dept-card {
    :deep(.el-card__header) {
      padding: 10px 12px;
    }
  }

  .card-header {
    .control-group {
      gap: 12px;
      margin-bottom: 10px;
    }

    .control-label {
      font-size: 12px;
    }

    .control-icon {
      font-size: 12px;
    }

    .control-tip {
      font-size: 10px;
      padding: 4px 8px;
    }

    .tip-icon {
      font-size: 11px;
    }
  }

  .tree-container {
    max-height: 250px;
    padding: 10px 12px;
  }

  .enhanced-tree {
    :deep(.el-tree-node) {
      .el-tree-node__content {
        height: 28px;
        padding: 0 4px;
      }

      .el-tree-node__expand-icon {
        font-size: 10px;
        padding: 1px;
      }

      .el-tree-node__label {
        font-size: 12px;
        padding: 2px 4px;

        &::before {
          width: 2px;
          height: 2px;
        }
      }

      .el-checkbox {
        margin-right: 4px;

        :deep(.el-checkbox__input) {
          .el-checkbox__inner {
            width: 12px;
            height: 12px;

            &::after {
              width: 2px;
              height: 4px;
            }
          }
        }
      }
    }

    :deep(.el-tree-node__children) {
      .el-tree-node {
        .el-tree-node__content {
          margin-left: 10px;

          &::before {
            left: -5px;
            width: 3px;
          }
        }
      }
    }

    :deep(.el-tree__empty-block) {
      padding: 30px 0;

      .el-tree__empty-text {
        font-size: 12px;

        &::before {
          font-size: 24px;
          margin-bottom: 4px;
        }
      }
    }
  }

  .dialog-footer {
    padding: 12px 16px;
    gap: 8px;
  }

  .enhanced-tag {
    padding: 4px 8px;
    font-size: 12px;
  }

  .tag-icon {
    font-size: 11px;
  }
}
</style>
